package com.example.techstudy.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author: ls
 * @date: 2022/3/23
 * @description:
 */
@Configuration
public class ThreadConfig {

    @Bean("threadPoolTaskExecutor")
    public ThreadPoolTaskExecutor getThreadExecutor(){
        int coreSize = Runtime.getRuntime().availableProcessors();
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize(coreSize);
        taskExecutor.setMaxPoolSize(coreSize);
        taskExecutor.setKeepAliveSeconds(300);
        taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        taskExecutor.setThreadNamePrefix("自定义线程");
        taskExecutor.setQueueCapacity(100);
        return taskExecutor;
    }
}
